#import "SEGProfileView.h"

@implementation SEGProfileViewModel
-(instancetype)init{
    if (self == [super init]) {
        _ico = @"";
        _sex = @"";
        _nickname = @"";
        _communityName = @"";
        _authType = @"";
        _level = @"";
        _desc = @"";
    }
    return self;
}
@end

@implementation SEGProfileViewLayout
- (instancetype)initWithData:(SEGProfileViewModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segProfileViewLayout];
    return self;
}

-(void)segProfileViewLayout{
    //高度固定
    _totalHeight = kProfileViewHeight + kStatusbarHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _nikeNameLabelHeight = 0;
    _nikeNameLabelTextLayout = nil;
    _placeLabelHeight = 0;
    _placeLabelTextLayout = nil;
    _descLabelHeight = 0;
    _descLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    _nikeNameLabelTextLayout = [self layoutWithString:_data.nickname FontSize:25 color:kColor_1];
    _placeLabelTextLayout = [self layoutWithString:_data.communityName FontSize:15 color:kColor_3];
//    _descLabelTextLayout = [self layoutWithString:_data.desc FontSize:13 color:kColor_3];
}

-(SEGTextLayout*)layoutWithString:(NSString*)string FontSize:(CGFloat)size color:(UIColor*)color{
    if (string.length == 0) return nil;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    text.yy_color = color;
    text.yy_font = [UIFont systemFontOfSize:size];
    
    SEGTextContainer *container = [SEGTextContainer containerWithSize:CGSizeMake(LYScreenWidth(), CGFLOAT_MAX)];
    return [SEGTextLayout layoutWithContainer:container text:text];
}
@end

@implementation SEGProfileView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kProfileViewHeight;
    }
    self = [super initWithFrame:frame];
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.headImageView];
    [self addSubview:self.sexImageView];
    [self addSubview:self.levelImageView];
    [self addSubview:self.authorImageView];
    [self addSubview:self.rightImageView];
    
    [self addSubview:self.nikeNameLabel];
    [self addSubview:self.placeLabel];
    [self addSubview:self.descLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(segClick)];
    [self addGestureRecognizer:tap];
    return self;
}

-(void)segClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(protocolClick)]) {
        [self.delegate protocolClick];
    }
}

-(void)setWithLayout:(SEGProfileViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    if(![self.nikeNameLabel.textLayout isEqual:layout.nikeNameLabelTextLayout]) self.nikeNameLabel.textLayout = layout.nikeNameLabelTextLayout;
    if(![self.placeLabel.textLayout isEqual:layout.placeLabelTextLayout]) self.placeLabel.textLayout = layout.placeLabelTextLayout;
    if(![self.descLabel.textLayout isEqual:layout.descLabelTextLayout]) self.descLabel.textLayout = layout.descLabelTextLayout;
    
    [self.headImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.ico]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")]];
    [self.levelImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.level]] placeholderImage:nil];

    NSString *imageName = @"";
    if ([_layout.data.sex isEqualToString:@"1"]) {
        imageName = SEGPicName(@"icon_woman");
    }else if ([_layout.data.sex isEqualToString:@"2"]) {
        imageName = SEGPicName(@"icon_man");
    }
    self.sexImageView.image = [UIImage imageWithContentsOfFile: imageName];
    
    // 认证
    // authType:0未认证，1认证，2认证服务者，3官方
    NSString *authImageName;
    SEGUserAuthStatus authStatus = [_layout.data.authType integerValue];
    switch (authStatus) {
        case SEGUserAuthStatus_Auth:
        {
            authImageName = @"icon_authenti";
            break;
        }
        case SEGUserAuthStatus_AuthServer:
        {
            authImageName = @"icon_servant";
            break;
        }
        case SEGUserAuthStatus_AuthOfficial:
        {
            authImageName = @"icon_official";
            break;
        }
        case SEGUserAuthStatus_NotAuth:
        {
            break;
        }
        default:
            break;
    }
    if (authImageName) {
        self.authorImageView.hidden = NO;
        self.authorImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(authImageName)];
    }else {
        self.authorImageView.hidden = YES;
    }
    
    self.rightImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
    [self setupUI];
}

//界面布局
-(void)setupUI{
    //箭头
    [self.rightImageView sizeToFit];
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).mas_offset(kStatusbarHeight / 2.0);
        make.right.equalTo(self).mas_offset(-15);
    }];

    //昵称、小区等
    [self.nikeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rightImageView).mas_offset(-15);
        make.left.equalTo(self.mas_left).mas_offset(15);
    }];
    
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nikeNameLabel.mas_bottom).mas_offset(9);
        make.left.equalTo(self.mas_left).mas_offset(15);
    }];
//    self.nikeNameLabel.backgroundColor = kColor_bg;
//    self.placeLabel.backgroundColor = kColor_T;
    [self segConstrationAuthorAndLevelIcon];
    
    //头像
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rightImageView);
        make.right.equalTo(self.rightImageView.mas_left).mas_offset(-15);
        make.width.height.mas_equalTo(55);
    }];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 5.f;
    
    [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headImageView.mas_right);
        make.bottom.equalTo(self.headImageView.mas_bottom);
        make.width.height.mas_equalTo(15);
    }];
    self.sexImageView.layer.masksToBounds = YES;
    self.sexImageView.layer.cornerRadius = 15/2.0;
    //self.sexImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    //self.sexImageView.layer.borderWidth = 1.0;
}

-(void)segConstrationAuthorAndLevelIcon{
    [self.authorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nikeNameLabel.mas_right).mas_offset(5);
        make.centerY.equalTo(self.nikeNameLabel);
    }];
    
    [self.levelImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nikeNameLabel);
        make.width.height.mas_equalTo(16);
        if (self.authorImageView.isHidden) {
            make.left.equalTo(self.nikeNameLabel.mas_right).mas_offset(5);
        }else{
            make.left.equalTo(self.authorImageView.mas_right).mas_offset(5);
        }
    }];
}

#pragma mark - getter
-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _headImageView;
}

-(UIImageView *)sexImageView{
    if (!_sexImageView) {
        _sexImageView = [[UIImageView alloc]init];
        _sexImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _sexImageView.contentMode = UIViewContentModeScaleAspectFill;
        _sexImageView.layer.masksToBounds = YES;
        //_sexImageView.layer.cornerRadius = 0.5;
        _sexImageView.hidden = YES;
    }
    return _sexImageView;
}

-(UIImageView *)levelImageView{
    if (!_levelImageView) {
        _levelImageView = [[UIImageView alloc]init];
        _levelImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _levelImageView.contentMode = UIViewContentModeScaleAspectFill;
        _levelImageView.layer.masksToBounds = YES;
        //_levelImageView.layer.cornerRadius = 0.5;
    }
    return _levelImageView;
}

-(UIImageView *)authorImageView{
    if (!_authorImageView) {
        _authorImageView = [[UIImageView alloc]init];
        _authorImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _authorImageView.contentMode = UIViewContentModeScaleAspectFill;
        _authorImageView.layer.masksToBounds = YES;
        //_authorImageView.layer.cornerRadius = 0.5;
    }
    return _authorImageView;
}

-(UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc]init];
        _rightImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _rightImageView.contentMode = UIViewContentModeScaleAspectFill;
        _rightImageView.layer.masksToBounds = YES;
        //_rightImageView.layer.cornerRadius = 0.5;
    }
    return _rightImageView;
}


-(SEGLabel *)nikeNameLabel{
    if (!_nikeNameLabel) {
        _nikeNameLabel = [[SEGLabel alloc]init];
        _nikeNameLabel.userInteractionEnabled = NO;
        _nikeNameLabel.textAlignment = NSTextAlignmentCenter;
        _nikeNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
//        _nikeNameLabel.displaysAsynchronously = YES;
        _nikeNameLabel.ignoreCommonProperties = YES;
        _nikeNameLabel.fadeOnHighlight = NO;
        _nikeNameLabel.fadeOnAsynchronouslyDisplay = NO;
        //__weak typeof(self) _self = self;
        //_nikeNameLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(protocolnikeNameLabel:)]) {
        //[_self.delegate protocolnikeNameLabel:_self.nikeNameLabel];
        //}
        //};
    }
    return _nikeNameLabel;
}

-(SEGLabel *)placeLabel{
    if (!_placeLabel) {
        _placeLabel = [[SEGLabel alloc]init];
        _placeLabel.userInteractionEnabled = NO;
        _placeLabel.textAlignment = NSTextAlignmentCenter;
        _placeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
//        _placeLabel.displaysAsynchronously = YES;
        _placeLabel.ignoreCommonProperties = YES;
        _placeLabel.fadeOnHighlight = NO;
        _placeLabel.fadeOnAsynchronouslyDisplay = NO;
        //__weak typeof(self) _self = self;
        //_placeLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(protocolplaceLabel:)]) {
        //[_self.delegate protocolplaceLabel:_self.placeLabel];
        //}
        //};
    }
    return _placeLabel;
}

-(SEGLabel *)descLabel{
    if (!_descLabel) {
        _descLabel = [[SEGLabel alloc]init];
        _descLabel.userInteractionEnabled = NO;
        _descLabel.textAlignment = NSTextAlignmentCenter;
        _descLabel.textVerticalAlignment = SEGTextVerticalAlignmentBottom;
//        _descLabel.displaysAsynchronously = YES;
        _descLabel.ignoreCommonProperties = YES;
        _descLabel.fadeOnHighlight = NO;
        _descLabel.fadeOnAsynchronouslyDisplay = NO;
        //__weak typeof(self) _self = self;
        //_descLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(protocoldescLabel:)]) {
        //[_self.delegate protocoldescLabel:_self.descLabel];
        //}
        //};
    }
    return _descLabel;
}


@end

